package LeetCode.leetcode513;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {

    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int res = 0;
        TreeNode node;
        
        //添加第一个
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> lines = new ArrayList<>();
            int length = queue.size();
            while (length > 0) {
                node = queue.poll();
                assert node != null;
                lines.add(node.val);
                if (node.left != null)
                    queue.offer(node.left);
                if (node.right != null)
                    queue.offer(node.right);
                length--;
            }
            res = lines.get(0);
        }
        return res;
    }
}
